package com.pluginsacceptor.plugin.formula.datetime;


	
	
	import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Observer;

	import javax.swing.JOptionPane;

	import org.apache.log4j.Logger;

	import com.pluginsacceptor.plugin.formula.MathsTrigo.support.CellPositionParser;
import com.pluginsacceptor.plugin.formula.MathsTrigo.support.DataChooser;
import com.pluginsacceptor.plugin.formula.MathsTrigo.support.DataListener;
import com.pluginsacceptor.plugin.formula.MathsTrigo.support.DataSender;

	public abstract class AbstractListener2 implements ActionListener{

	    /** Logger */
	    private static final Logger LOG = Logger.getLogger(AbstractListener2.class);

	    /**
	     * Observable
	      */
	    private final DataListener dataListener;

	    /** */
	    private final DataSender dataSender;

	    /** */
	    private DataChooser dataChooser;

	    /**
	     * Constructor
	     * @param dataListener
	     */
	    public AbstractListener2(final DataListener dataListener, final DataSender dataSender) {
	        super();
	        this.dataListener = dataListener;
	        this.dataSender = dataSender;
	    }

	    @Override
		public void actionPerformed(final ActionEvent e) {

	        final String resultat = JOptionPane.showInputDialog(null, "Set resultat cell please",
	        		"title", JOptionPane.QUESTION_MESSAGE);
	        //todo add a parser A2 = xy

	        final CellPositionParser parser = new CellPositionParser(resultat);

	        if (parser.validate()) {
	            this.dataChooser = new DataChooser();
	            final Observer observer = dataChooser.getObserver();
	            this.dataListener.addObserver(observer);

	            dataChooser.setButtonListener(
	                new ActionListener() {
	                    @Override
	                    public void actionPerformed(final ActionEvent e) {

	                        //
	                        //
	                        final String result =  calculate();
	                        dataSender.sendDate(
	                            parser.getRow(),
	                            parser.getColumn(),
	                            String.valueOf(result)
	                        );
	                        dataChooser.dispose();
	                        dataListener.deleteObserver(observer);
	                    }
	                }
	            );
	            dataChooser.setVisible(true);
	        } else {
	            warningPopup(resultat);
	        }
		}

	    private static void warningPopup(final String resultat) {
	        LOG.warn("Invalid Cellule resultat " + resultat);
	        JOptionPane.showMessageDialog(
	            null,
	            "Invalid Cellule resultat " + resultat + " (example 'A 1')",
	            "warning",
	            JOptionPane.ERROR_MESSAGE
	        );
	    }


	    /**
	     * @return user data if exist
	     */
	    public String[] getUserData() {
	        String[] data;
	        if (this.dataChooser == null) {
	            data = new String[] {};
	        } else {
	            final List<String> dataList = this.dataChooser.getMathData();
	            data = new String[dataList.size()];
	            for (int cnt = dataList.size(); --cnt >=0;) {
	                data[cnt] = dataList.get(cnt);
	            }
	        }
	        return data;
	    }


	    //
	    protected abstract String calculate();
	}

